<?php
/**
 * Created by PhpStorm.
 * User: Martin.Wu
 * Date: 2024/7/14
 * Time: 11:04
 *
 **/
namespace common;

use L;

class commonUpdateTrait
{
    public static function update($table, $data, $id, $field = 'id', $conn = 'warehouse')
    {
        if (isset($data['json'])) {
            $json = $data['json'];
            foreach ($json as $key => $val) {
                $val = is_string($val) ? addslashes($val) : $val;
                $jsonUpdata[] = "\"$.{$key}\", '{$val}'";
            }
            $stringArr[] = " `data`= JSON_SET(`data`, " . implode(",", $jsonUpdata) . ")";
            unset($data['json']);
        }
        foreach ($data as $k => $v) {
            $v = is_array($v) ? json_encode($v) : $v;
            $stringArr[] = " `{$k}` = '{$v}' ";
        }
        $string = implode(",", $stringArr);
        $sql = "UPDATE `{$table}` SET {$string} WHERE 1 AND {$field} = '{$id}';";
        L::sql($sql, $conn);
    }
}
